package cst.a722wr.zyfypt_722wr.model;



import android.util.Log;

import java.util.List;

import  cst.a722wr.zyfypt_722wr.Bean.ArticleBean;
import  cst.a722wr.zyfypt_722wr.Bean.LoginBean;
import  cst.a722wr.zyfypt_722wr.Bean.ProjectBean;
import  cst.a722wr.zyfypt_722wr.Bean.TcaseBean;
import  cst.a722wr.zyfypt_722wr.Bean.TwareBean;
import  cst.a722wr.zyfypt_722wr.Bean.VideoBean;
import  cst.a722wr.zyfypt_722wr.Bean.VideoTopicBean;
import  cst.a722wr.zyfypt_722wr.iface.ResService;
import  cst.a722wr.zyfypt_722wr.iface.wrIface;
import  cst.a722wr.zyfypt_722wr.iface.wrListener;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class ResModel<T> implements wrIface {

    private static final String BASE_URL = "http://amicool.neusoft.edu.cn/";
    private Retrofit retrofit;
    private ResService service;
    private String SESSION_ID;

    public ResModel(String type, String s_id) {
        if(type.equals("gson")){
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        else{
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
        }
        service = retrofit.create(ResService.class);
        SESSION_ID = s_id;
    }

    public ResModel(String type) {
        if(type.equals("gson")){
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        else{
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(ScalarsConverterFactory.create())
                    .build();
        }
        service = retrofit.create(ResService.class);
    }

    @Override
    public void login(String u, String p, final wrListener l) {
        Call<LoginBean> call = service.login(u, p);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                l .onFail(t.toString());
            }
        });
    }

    @Override
    public void logout(String sessionID, final wrListener l) {
        Call<String> call = service.logout(sessionID);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void reg(String u, String p, String telephone, int roleid, String email, String realname, final wrListener l) {
        Call<String> call = service.reg(u, p, telephone, roleid, realname, email);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void getList(String mod, int page, final wrListener l) {

            switch (mod) {
                case "video_new":
                    Call<List<VideoBean>> call_video_new = service.getVideoList("video", page, SESSION_ID);
                    Log.d("MainActivity", " sessionID is " + SESSION_ID);
                    call_video_new.enqueue(new Callback<List<VideoBean>>() {
                        @Override
                        public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                            l.onResponse(response.body());
                        }

                        @Override
                        public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                            l.onFail(t.toString());
                        }
                    });
                    break;

                case "video_topic":
                    Call<List<VideoTopicBean>> call_video_topic = service.getVideoTopicList("video", page, SESSION_ID);
                    call_video_topic.enqueue(new Callback<List<VideoTopicBean>>() {
                        @Override
                        public void onResponse(Call<List<VideoTopicBean>> call, Response<List<VideoTopicBean>> response) {
                            l.onResponse(response.body());
                        }

                        @Override
                        public void onFailure(Call<List<VideoTopicBean>> call, Throwable t) {
                            l.onFail(t.toString());
                        }
                    });
                    break;

                case "article":
                    Call<List<ArticleBean>> call_article = service.getArticleList(mod, page, SESSION_ID);
                    call_article.enqueue(new Callback<List<ArticleBean>>() {
                        @Override
                        public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                            l.onResponse(response.body());
                        }

                        @Override
                        public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                            l.onFail(t.toString());
                        }
                    });
                    break;

                case "tware":
                    Call<List<TwareBean>> call_tware = service.getTwareList(mod, page, SESSION_ID);
                    call_tware.enqueue(new Callback<List<TwareBean>>() {
                        @Override
                        public void onResponse(Call<List<TwareBean>> call, Response<List<TwareBean>> response) {
                            l.onResponse(response.body());
                        }

                        @Override
                        public void onFailure(Call<List<TwareBean>> call, Throwable t) {
                            l.onFail(t.toString());
                        }
                    });
                    break;

                case "tcase":
                    Call<List<TcaseBean>> call_tcase = service.getTcaseList(mod, page, SESSION_ID);
                    call_tcase.enqueue(new Callback<List<TcaseBean>>() {
                        @Override
                        public void onResponse(Call<List<TcaseBean>> call, Response<List<TcaseBean>> response) {
                            l.onResponse(response.body());
                        }

                        @Override
                        public void onFailure(Call<List<TcaseBean>> call, Throwable t) {
                            l.onFail(t.toString());
                        }
                    });
                    break;

                case "project":
                    Call<List<ProjectBean>> call_project = service.getProjectList(mod, page, SESSION_ID);
                    call_project.enqueue(new Callback<List<ProjectBean>>() {
                        @Override
                        public void onResponse(Call<List<ProjectBean>> call, Response<List<ProjectBean>> response) {
                            l.onResponse(response.body());
                        }

                        @Override
                        public void onFailure(Call<List<ProjectBean>> call, Throwable t) {
                            l.onFail(t.toString());
                        }
                    });
                    break;

                default:
                    break;
            }
    }

    @Override
    public void getUserList(String mod, int page, String sessionID, int userid,final wrListener l) {
        switch (mod){

            case "video":
                Call<List<VideoBean>> call_video = service.getFocusVideoList(mod, page, sessionID, userid);
                call_video.enqueue(new Callback<List<VideoBean>>() {
                    @Override
                    public void onResponse(Call<List<VideoBean>> call, Response<List<VideoBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<VideoBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;

            case "tware":
                Call<List<TwareBean>> call_tware = service.getFocusTwareList(mod, page, sessionID, userid);
                call_tware.enqueue(new Callback<List<TwareBean>>() {
                    @Override
                    public void onResponse(Call<List<TwareBean>> call, Response<List<TwareBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<TwareBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;

            case "article":
                Call<List<ArticleBean>> call_article = service.getFocusArticleList(mod, page, sessionID, userid);
                call_article.enqueue(new Callback<List<ArticleBean>>() {
                    @Override
                    public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;

            case "tcase":
                Call<List<TcaseBean>> call_tcase = service.getFocusTcaseList(mod, page, sessionID, userid);
                call_tcase.enqueue(new Callback<List<TcaseBean>>() {
                    @Override
                    public void onResponse(Call<List<TcaseBean>> call, Response<List<TcaseBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<TcaseBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;

            case "project":
                Call<List<ProjectBean>> call_project = service.getFocusProjectList(mod, page, sessionID, userid);
                call_project.enqueue(new Callback<List<ProjectBean>>() {
                    @Override
                    public void onResponse(Call<List<ProjectBean>> call, Response<List<ProjectBean>> response) {
                        l.onResponse(response.body());
                    }

                    @Override
                    public void onFailure(Call<List<ProjectBean>> call, Throwable t) {
                        l.onFail(t.toString());
                    }
                });
                break;

            default:
                break;
        }
    }
}
